package com.spica.spicaschool.ui.sport

import android.annotation.SuppressLint
import android.content.Intent
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.drakeet.multitype.MultiTypeAdapter
import com.fondesa.recyclerviewdivider.dividerBuilder
import com.spica.spicaschool.base.BaseFragment
import com.spica.spicaschool.bean.SportMotionRecord
import com.spica.spicaschool.databinding.FragmentSportTimeBinding
import com.spica.spicaschool.ui.sportMap.SportMapActivity
import com.spica.spicaschool.viewmodel.SportViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * 计时模式
 */
@Suppress("unused")
@AndroidEntryPoint
class SportTimeFragment : BaseFragment<FragmentSportTimeBinding>() {

  private val viewModel: SportViewModel by viewModels()


  private val items: ArrayList<SportMotionRecord> = arrayListOf()

  private val sportRecordAdapter = MultiTypeAdapter(items)

  override fun setupViewBinding(
    inflater: LayoutInflater,
    container: ViewGroup?
  ): FragmentSportTimeBinding = FragmentSportTimeBinding.inflate(inflater, container, false)


  override fun init() {

    requireContext()
      .dividerBuilder()
      .size(12, TypedValue.COMPLEX_UNIT_DIP)
      .asSpace()
      .showLastDivider()
      .showFirstDivider()
      .build()
      .addTo(viewBinding.rvRecords)

    //设置点击
    viewBinding.btnMap.setOnClickListener {
      startActivity(Intent(requireActivity(), SportMapActivity::class.java))
    }

    sportRecordAdapter.register(SportRecordDelegate(requireContext()))
    viewBinding.rvRecords.adapter = sportRecordAdapter

    initData()
  }


  /**
   * 载入数据
   */
  @SuppressLint("NotifyDataSetChanged")
  private fun initData() {
    viewModel.sportMotionRecordLiveData.observe(this, {
      items.clear()
      items.addAll(it)
      sportRecordAdapter.notifyDataSetChanged()
    })

  }

}
